﻿using System;
using Assets.AnimationPlayer.Scripts.Util;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.AnimationPlayer.Scripts.Prefabs
{
    public class MessageBox : MonoBehaviour
    {

        public static string PrefabsName = "MessageBox";
        public static string PrefabsPath = "YioksFootball/Form/MessageBox";

        public GameObject Panel;
        public GameObject BtnSave;
        public GameObject BtnCancel;

        public Text MessageText;
        public Text BtnSaveText;
        public Text BtnCancelText;

        public GameObject BtnSaveBackground;
        public GameObject BtnCancelBackground;

        public Image ForwardSaveBackground;
        public Image ForwardCancelBackground;

        private YioksCallBack.CallBackFun _saveCallBackFun;
        private YioksCallBack.CallBackFun _cancelCallBackFun;

        private int _selectId;
        // Use this for initialization
        internal void Start()
        {
            name = PrefabsName;
            transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
            transform.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 0);
            YioksTriggerListener.Get(transform).OnClick = __eve_close;
            YioksTriggerListener.Get(BtnSave).OnClick = __eve_save;
            YioksTriggerListener.Get(BtnCancel).OnClick = __eve_close;
            YioksTriggerListener.Get(Panel).OnClick = __eve_null;
        }
        private static void __eve_null(GameObject go)
        {

        }

        private void __eve_save(GameObject go)
        {
            transform.gameObject.SetActive(false);
            if (_saveCallBackFun != null)
            {
                _saveCallBackFun.Invoke();
            }
        }

        public void ResetSelect()
        {
            _selectId = 1;
            BtnSaveBackground.SetActive(false);
            YioksTool.SetAlpha(ForwardSaveBackground, 0.5f);
            BtnCancelBackground.SetActive(true);
            YioksTool.SetAlpha(ForwardCancelBackground, 1.0f);
        }


        private void __changeSelect()
        {
            _selectId = _selectId == 0 ? 1 : 0;
            if (_selectId == 0)
            {
                BtnSaveBackground.SetActive(true);
                YioksTool.SetAlpha(ForwardSaveBackground, 1.0f);
                BtnCancelBackground.SetActive(false);
                YioksTool.SetAlpha(ForwardCancelBackground, 0.5f);
            }
            else
            {
                BtnSaveBackground.SetActive(false);
                YioksTool.SetAlpha(ForwardSaveBackground, 0.5f);
                BtnCancelBackground.SetActive(true);
                YioksTool.SetAlpha(ForwardCancelBackground, 1.0f);
            }
        }

        #region 关闭事件
        private void __eve_close(GameObject go)
        {
            transform.gameObject.SetActive(false);
            if (_cancelCallBackFun != null)
            {
                _cancelCallBackFun.Invoke();
            }
        }
        #endregion

        public void bing_obj(string message, YioksCallBack.CallBackFun callBack, YioksCallBack.CallBackFun closeBackFun = null)
        {
            MessageText.text = message;
            _saveCallBackFun = callBack;
            _cancelCallBackFun = closeBackFun;
        }


        private int _arrowH; //-1,0,1分别代表左中右
        private int _arrowV; //1,0,-1分别代表上中下
        internal void Update()
        {
            if (!gameObject.activeInHierarchy) return;

            if (Input.anyKeyDown)
            {
                if (Input.GetKey(KeyCode.JoystickButton0) ||
                    Input.GetKey(KeyCode.Joystick1Button0) ||
                    Input.GetKey(KeyCode.Joystick2Button0) ||
                    Input.GetKey(KeyCode.Joystick3Button0) ||
                    Input.GetKey(KeyCode.Joystick4Button0) ||
                    Input.GetKey(KeyCode.Joystick5Button0) ||
                    Input.GetKey(KeyCode.Joystick6Button0) ||
                    Input.GetKey(KeyCode.Joystick7Button0) ||
                    Input.GetKey(KeyCode.Joystick8Button0) || Input.GetKey(KeyCode.Return) || Input.GetKey((KeyCode)10))
                {
                    if (_selectId == 0)
                    {
                        __eve_save(null);
                    }
                    else
                    {
                        __eve_close(null);
                    }
                }

                if (Input.GetKey(KeyCode.JoystickButton1) ||
                  Input.GetKey(KeyCode.Joystick1Button1) ||
                  Input.GetKey(KeyCode.Joystick2Button1) ||
                  Input.GetKey(KeyCode.Joystick3Button1) ||
                  Input.GetKey(KeyCode.Joystick4Button1) ||
                  Input.GetKey(KeyCode.Joystick5Button1) ||
                  Input.GetKey(KeyCode.Joystick6Button1) ||
                  Input.GetKey(KeyCode.Joystick7Button1) ||
                  Input.GetKey(KeyCode.Joystick8Button1) || Input.GetKey(KeyCode.Escape))
                {
                    __eve_close(null);
                }

                if (Input.GetKey(KeyCode.JoystickButton3) ||
                    Input.GetKey(KeyCode.Joystick1Button3) ||
                    Input.GetKey(KeyCode.Joystick2Button3) ||
                    Input.GetKey(KeyCode.Joystick3Button3) ||
                    Input.GetKey(KeyCode.Joystick4Button3) ||
                    Input.GetKey(KeyCode.Joystick5Button3) ||
                    Input.GetKey(KeyCode.Joystick6Button3) ||
                    Input.GetKey(KeyCode.Joystick7Button3) ||
                    Input.GetKey(KeyCode.Joystick8Button3) || Input.GetKey(KeyCode.Space))
                {
                    __eve_close(null);
                }
            }

            //设置选择 项 
            if (Math.Abs(Input.GetAxis("Horizontal")) > 0)
            {
                if (Input.GetAxis("Horizontal") > 0)
                {
                    if (_arrowH == 1)
                    {
                        return;
                    }
                    else
                    {
                        _arrowH = 1;
                    }
                    //向右
                    __changeSelect();
                }
                else
                {
                    if (_arrowH == -1)
                    {
                        return;
                    }
                    else
                    {
                        _arrowH = -1;
                    }
                    //向左
                    __changeSelect();
                }
            }
            else if (Math.Abs(Input.GetAxis("Horizontal")) <= 0)
            {
                _arrowH = 0;
            }

            if (Math.Abs(Input.GetAxis("Vertical")) > 0)
            {
                if (Input.GetAxis("Vertical") > 0)
                {
                    if (_arrowV == 1)
                    {
                        return;
                    }
                    else
                    {
                        _arrowV = 1;
                    }

                    //向上
                    __changeSelect();
                }
                else
                {
                    if (_arrowV == -1)
                    {
                        return;
                    }
                    else
                    {
                        _arrowV = -1;
                    }

                    //向下
                    __changeSelect();
                }
            }
            else if (Math.Abs(Input.GetAxis("Vertical")) <= 0)
            {
                _arrowV = 0;
            }

        }


    }
}